package com.geekchique.runningbuddy.controller;

import android.graphics.BitmapFactory;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import com.geekchique.runningbuddy.R;

import com.geekchique.runningbuddy.activity.MapOfJog;
import com.geekchique.runningbuddy.view.MapOfJogView;
import com.geekchique.runningbuddy.view.mapofjog.JogRouteImpl;
import com.geekchique.runningbuddy.view.mapofjog.MapOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayController;
import com.google.android.maps.Point;
import com.google.googlenav.DrivingDirection;
import com.google.googlenav.map.MapPoint;

public class MapOfJogControllerImpl implements IMapOfJogController{

	private MapOfJogView mapOfJogView;
	private MapController mapController;
	private MapOfJog mapOfJogActivity;
	private Spinner pointCollectionList;
	private String[][] spinnerListModel;
	private DrivingDirection myDD;
	private OverlayController overlayController;
	
	public MapOfJogControllerImpl(MapOfJog mapOfJog) {
		this.mapOfJogActivity = mapOfJog;
	}

	public void createMapOfJogView(Button goButton, Spinner pointCollection, int spinnerDorpDownItems, MapView mapView, String[][] locations) {
		this.mapOfJogView = new MapOfJogView(mapOfJogActivity, this, goButton, mapView);
		this.pointCollectionList = pointCollection;
		this.mapController = mapView.getController();
		this.spinnerListModel = locations;
		
		setSpinnerPresenter(populateSpinnerListPresenter(mapOfJogActivity, spinnerDorpDownItems, locations));
		
		this.overlayController = mapView.createOverlayController();
		
		MapOverlay drivingDirectionsOverlay = 
			new MapOverlay(
					this.mapOfJogActivity,
					BitmapFactory.decodeResource(this.mapOfJogActivity.getResources(), R.drawable.icon),
					BitmapFactory.decodeResource(this.mapOfJogActivity.getResources(), R.drawable.mappin_red),
					BitmapFactory.decodeResource(this.mapOfJogActivity.getResources(), R.drawable.lower_left_info)
					);
		
		overlayController.add(drivingDirectionsOverlay, true);
//		submitBtn.setOnClickListener(submitClickedListener);
		
		
	}

	private void setSpinnerPresenter(ArrayAdapter<CharSequence> adapter) {
	      this.pointCollectionList.setAdapter(adapter);
	      gotoCurrentlySelected();
	}

	private ArrayAdapter<CharSequence> populateSpinnerListPresenter(MapActivity mapOfJogActivity, int spinnerDropDownListID, String[][] locations) {
		ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>( mapOfJogActivity, spinnerDropDownListID);
		    
		for (int i = 0; i < locations.length; i++){
			adapter.addObject(locations[i][0]);
		}
	      
		adapter.setDropDownViewResource(spinnerDropDownListID);
		return adapter;
	}

	public void gotoCurrentlySelected() {
		
		Point p = getMapPoint(this.pointCollectionList.getSelectedItemPosition());
	      
		mapController.animateTo(p);
		mapController.zoomTo(18);
	      
		if (mapOfJogView.isSatellite())
		{
			mapOfJogView.invalidateMapView();
		}else{
			mapOfJogView.toggleSatellite();
		}
		
		MapPoint mpFrom = new MapPoint(55846809, -4229920); // 37.423157,-122.085008
		MapPoint mpTo = new MapPoint(55846904,-4232676); // 37.495999,-122.457508
		MapOfJogControllerImpl.this.mapOfJogActivity.updateOnScreenDirections(mpFrom,"", mpTo, "");
	      
	}

	private Point getMapPoint(int pos) {
		String[] loc = spinnerListModel[pos][1].split(",");
	      
		double lat = Double.parseDouble(loc[1]);
		double lon = Double.parseDouble(loc[0]);
	      
		return new Point((int) (lat * 1000000), (int) (lon * 1000000));
	}

	public void zoomTo(int zoomLevel) {
		this.mapController.zoomTo(zoomLevel);
	}

	public JogRouteImpl getCurrentJogRoute() {
		// TODO Auto-generated method stub
		return null;
	}
	
}
